package com.jeesite.modules.dp.service.kecheng;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.dp.dao.kecheng.DpKechengDao;
import com.jeesite.modules.dp.entity.kecheng.DpKecheng;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 课程管理Service
 *
 * @author hancing
 * @version 2024-12-02
 */
@Service
public class DpKechengService extends CrudService<DpKechengDao, DpKecheng> {

    /**
     * 获取单条数据
     *
     * @param dpKecheng
     * @return
     */
    @Override
    public DpKecheng get(DpKecheng dpKecheng) {
        return super.get(dpKecheng);
    }

    /**
     * 查询分页数据
     *
     * @param dpKecheng 查询条件
     * @param dpKecheng page 分页对象
     * @return
     */
    @Override
    public Page<DpKecheng> findPage(DpKecheng dpKecheng) {
//		String uno = UserUtils.getUser().getLoginCode();
//		dpKecheng.sqlMap().add("extWhere", "AND a.id in (select kcid from dp_kcgoumai where uno='" + uno + "'"); //
        return super.findPage(dpKecheng);
    }

    public Page<DpKecheng> frontfindPage(Page<DpKecheng> page, DpKecheng dpKecheng) {
        // 设置分页参数，传递给MyEntityDao的查询方法
        page.setList(dao.frontfindPage(dpKecheng));
        return page;
    }

    /**
     * 查询列表数据
     *
     * @param dpKecheng
     * @return
     */
    @Override
    public List<DpKecheng> findList(DpKecheng dpKecheng) {
        return super.findList(dpKecheng);
    }

    private void deletePrevFiles(String bizKey, String bizType) {
        List<FileUpload> files = FileUploadUtils.findFileUpload(bizKey, bizType);
        if (files != null && files.size() > 1) {
            for (int i = 0; i < files.size(); i++) {
                if (i < files.size() - 1) {
                    files.get(i).setStatus("1");
                    FileUploadUtils.getFileUploadService().updateStatus(files.get(i));
                }
            }
        }
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param dpKecheng
     */
    @Override
    @Transactional
    public void save(DpKecheng dpKecheng) {
        super.save(dpKecheng);
        // 保存上传图片
        FileUploadUtils.saveFileUpload(dpKecheng, dpKecheng.getId(), "dpKecheng_image");
        deletePrevFiles(dpKecheng.getId(), "dpKecheng_image");
        List<FileUpload> files = FileUploadUtils.findFileUpload(dpKecheng.getId(), "dpKecheng_image");
        if (files != null && !files.isEmpty()) {
            dpKecheng.setKcimg(files.getLast().getFileUrl());
            super.update(dpKecheng);
        }
    }

    /**
     * 更新状态
     *
     * @param dpKecheng
     */
    @Override
    @Transactional
    public void updateStatus(DpKecheng dpKecheng) {
        super.updateStatus(dpKecheng);
    }

    /**
     * 删除数据
     *
     * @param dpKecheng
     */
    @Override
    @Transactional
    public void delete(DpKecheng dpKecheng) {
        super.delete(dpKecheng);
    }

    @Transactional
    public void updaterealsell(@Param("id") String id) {
        dao.updaterealsell(id);
    }

}